<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/12
 * Time: 16:13
 */

namespace app\index\controller;


use think\Db;
use app\index\model\User as UserModel;
use think\facade\Request;

class User extends Commen
{
    //登录界面
    public function login()
    {
        return $this->fetch();
    }

    /**
     * 验证登录
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkLogin(Request $request)
    {
        $data = input("post.");//param获取当前请求的变量

        $map = [
            'username'=>$data['username'],
            'password'=>md5($data['password']),
        ];

        //查询用户信息
        $user = Db::name('user')
            ->where($map)
            ->find();
        if ($user && $user['id'])
        {
            if($user['status'] == '1')
            {
                session("user_name",$user['username']);
                session("user_id",$user['id']);
                return ['code'=>0, 'msg'=>'登录成功','data'=>['url'=>url('/')]];
            }
            if($user['status'] == '2')
            {
                return ['code'=>1, 'msg'=>'该账户已经被冻结！','data'=>[]];
            }
        }else{
            return ['code'=>1, 'msg'=>'没有找到该用户,请检查用户名或密码!','data'=>[]];
        }
    }


    //验证码
    public function yanzhengma(){
        session_start();
        Header("Content-type:image/PNG");
        $im = imagecreate(60, 25);
        $back = imagecolorallocate($im, 245, 245, 245);
        imagefill($im, 0, 0, $back);
        $vcodes = "";

        for($i = 0; $i < 4; $i++){
            $font = imagecolorallocate($im, rand(100, 255), rand(0, 100), rand(100, 255));
            $authnum = rand(0, 9);
            $vcodes .= $authnum;
            imagestring($im, 5, 9 + $i * 10, 5, $authnum, $font);
        }
        $_SESSION['VCODE'] = $vcodes;
        for($i=0;$i<200;$i++) {
            $randcolor = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
            imagesetpixel($im, rand()%60, rand()%25, $randcolor);
        }
        imagepng($im);
        imagedestroy($im);
    }


    //注册页面
    public function register()
    {
        return $this->fetch();
    }


    /**
     * 验证注册
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkRegister(Request $request)
    {
        $data = input("post.");

        //验证
        $info = UserModel::getFieldValue("id",['username'=>$data['username']]);
        if($info && $info['id']){
            return ['code'=>1, 'msg'=>'已经存在此用戶','data'=>[]];
        }

        $data['password'] = md5($data['password']);
        $res = UserModel::create($data);
        if ($res !== false){
            session("user_name",$data['username']);
            session("user_id",$res['id']);
            return ['code'=>0, 'msg'=>'注册成功','data'=>['url'=>url('/')]];
        }
        return ['code'=>1, 'msg'=>'注册失敗','data'=>[]];
    }

    /**
     * 注销登录
     * @return array
     */
    public function logout()
    {
        if(Request::isAjax())
        {
            session("user_name",NULL);
            session("user_id",NULL);
            return ['code'=>0, 'msg'=>'注销成功','data'=>['url'=>url('/')]];
        }
        return ['code'=>1, 'msg'=>'注销失败','data'=>[]];
    }
}